package com.maikang.exammall.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.maikang.exammall.entity.constant.RepositoryConstant;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Positive;

import static com.maikang.exammall.entity.Comment.TABLE_NAME;

@Entity
@Table(name = TABLE_NAME, schema = RepositoryConstant.APP_DB_TABLE_SCHEMA, catalog = RepositoryConstant.APP_DB_TABLE_SCHEMA)
@DynamicUpdate
@DynamicInsert
@SuperBuilder
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true, value = {"hibernateLazyInitializer", "handler"})
@NoArgsConstructor
@AllArgsConstructor
@Data
@EqualsAndHashCode(callSuper=true)
@ToString(callSuper = true)
@SQLDelete(sql = "update " + RepositoryConstant.APP_DB_TABLE_SCHEMA + "." + TABLE_NAME + " set is_deleted = 1 WHERE id = ?")
@Where(clause = " is_deleted = 0")
public class Comment extends AuditEntity<Long> {
    public static final String TABLE_NAME = "mkp_comment";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @ApiModelProperty("父评论id")
    private Long parentId;
    @Positive
    private Long pkgId;
    @NotBlank
    private String pkgName;
    @NotBlank
    private String content;
    // redundant
    @ApiModelProperty(hidden = true)
    private Long userId;
    @ApiModelProperty(hidden = true)
    private String username;
    // redundant
    @ApiModelProperty(hidden = true)
    private String userAvatarUrl;

    @Override
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    public Long getPrimaryKey() {
        return this.id;
    }

    @Override
    public void setPrimaryKey(Long id) {
        this.id = id;
    }
}
